package com.cskaoyan.config5;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

/**
 * @author stone
 * @date 2022/07/15 10:20
 */
@Configuration
@EnableConfigurationProperties(DataSourceProperties.class)
public class DataSourceConfiguration5 {

    //@Autowired
    //DataSourceProperties properties;
    DataSourceProperties properties;

    // 配置类的有参构造方法 →
    //    如果没有无参构造方法，那么在bean的实例化的时候会采用有参构造方法
    //    有参构造方法的形参，默认按照类型从容器中取出组件；如果该类型组件不止一个，通过@Qualifier做指定
    public DataSourceConfiguration5(DataSourceProperties properties) {
        this.properties = properties;
    }

    @Bean
    public DataSource dataSource5() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(properties.getDriverClassName());
        dataSource.setUrl(properties.getUrl());
        dataSource.setUsername(properties.getUsername());
        dataSource.setPassword(properties.getPassword());
        return dataSource;
    }
}
